package com.jet.core.exception;

import cn.hutool.json.JSONUtil;
import com.jet.core.result.ResultCode;
import com.jet.core.result.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.util.List;

/**
 * 异常处理类
 */
@RestControllerAdvice
@Slf4j
public class DefaultExceptionHandler {

    /**
     * 请求路径不存在异常处理，系统抛出
     *
     * @param e
     * @return
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public ResultJson<Void> handleNoHandlerFoundException(NoHandlerFoundException e) {
        return ResultJson.failure(ResultCode.NOT_FOUND.getCode(), e.getHttpMethod() + "请求路径不存在");
    }

    /**
     * 请求方式异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResultJson<Void> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return ResultJson.failure(ResultCode.BAD_REQUEST.getCode(), e.getMethod() + "请求方式不支持");
    }

    /**
     * 参数校验异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public ResultJson<Void> handleIllegalArgumentException(IllegalArgumentException e) {
        return ResultJson.failure(ResultCode.BAD_REQUEST.getCode(), e.getMessage());
    }

    /**
     * 参数绑定异常 处理参数校验异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler({BindException.class})
    public ResultJson<Void> MethodArgumentNotValidExceptionHandler(BindException e) {
        // 从异常对象中拿到ObjectError对象
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        return ResultJson.failure(ResultCode.BAD_REQUEST.getCode(), objectError.getDefaultMessage());
    }

    /**
     * RequestBody中参数验证错误处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultJson<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("requestBody参数错误,{}", e.getMessage(), e);
        List<ObjectError> errors = e.getBindingResult().getAllErrors();
        String tips = "参数不合法";
        if (!errors.isEmpty()) {
            tips = errors.get(0).getDefaultMessage();
        }
        return ResultJson.failure(ResultCode.BAD_REQUEST.getCode(), tips);
    }

    /**
     * 业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public ResultJson<Void> handleCustomException(CustomException e) {
        log.error("自定义运行异常，msg:{}", JSONUtil.toJsonStr(e.getResultJson()), e);
        if (e.getResultJson() == null) {
            return ResultJson.failure();
        }
        return ResultJson.failure(e.getResultJson().getCode(), e.getResultJson().getMsg());
    }

    /**
     * 系统异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResultJson<Void> handleSysException(Exception e) {
        log.error("系统异常：{}", e.getMessage(), e);
        return ResultJson.failure(ResultCode.SYSTEM_ERROR);
    }
}
